package fr.cie.blog.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;

import fr.cie.blog.beans.Style;
import fr.cie.blog.beans.User;
import fr.cie.blog.domainservice.IServiceConfig;
import fr.cie.blog.domainservice.IServiceStyle;

/**
 * @author AetM
 * 
 */
public class CoreAction extends GenericAction implements ServletRequestAware {

	// Dao Access for spring injection ...
	/**
	 * serviceConfig.
	 */
	private IServiceConfig serviceConfig;

	/**
	 * serviceStyle.
	 */
	private IServiceStyle serviceStyle;

	// serialization properties

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = 4001274599362401292L;

	// spring properties ..

	/**
	 * Log4J the logger.
	 */
	private static final Logger LOG = Logger.getLogger(CoreAction.class);
	/**
	 * request.
	 */
	private HttpServletRequest request;



	/**
	 * defaultStyle.
	 */
	private Style defaultStyle;

	/**
	 * styleList.
	 */
	private List<Style> styleList;

	// implicit web properties

	// g&s

	// spring properties ..

	/**
	 * Constructor.
	 * 
	 * @param pServiceConfig
	 *            serviceConfig
	 * @param pServiceStyle
	 *            serviceStyle
	 */
	public CoreAction(final IServiceConfig pServiceConfig,
			final IServiceStyle pServiceStyle) {
		super();
		serviceConfig = pServiceConfig;
		serviceStyle = pServiceStyle;

	}

	@Override
	public final void setServletRequest(final HttpServletRequest pRequest) {
		request = pRequest;

	}

	/**
	 * @return the defaultStyle
	 */
	public final Style getDefaultStyle() {
		return defaultStyle;
	}

	/**
	 * @return the styleList
	 */
	public final List<Style> getStyleList() {
		return styleList;
	}

	// IMPLEMENTS METHODS



	/**
	 * Boot Action.
	 * 
	 * @return SUCCESS
	 */
	public final String boot() {

		LOG.info("MB - ENTERING IN boot() no parameter");

		// Initialization of default style in APPLICATION Context
		final Map<String, Object> myContext = ActionContext.getContext()
				.getApplication();
		if (myContext.get("defaultStyle") == null) {
			// context is not completely initialized

			// initialization of styles
			Style red = serviceStyle.findById(1);
			if (red == null) {
				red = new Style();
				// if id is set, => Hibernate Exception because auto-increment
				// red.setId(1);
				red.setName("red");
				red.setStatus("default");
				serviceStyle.save(red);
			}

			Style pink = serviceStyle.findById(2);
			if (pink == null) {
				pink = new Style();
				// pink.setId(2);
				pink.setName("pink");
				pink.setStatus(null);
				serviceStyle.save(pink);
			}

			Style yellow = serviceStyle.findById(3);
			if (yellow == null) {
				yellow = new Style();
				// yellow.setId(3);
				yellow.setName("yellow");
				yellow.setStatus(null);
				serviceStyle.save(yellow);
			}

			// initialization of defaultStyle
			defaultStyle = serviceStyle.findDefaultStyle();
			myContext.put("defaultStyle", defaultStyle);
		}

		// Initialization of Config
		if (getConfig().getRootPath() == null) {
			// initialization of rootPath, cssPath and imgPath
			final ServletContext context = ServletActionContext
					.getServletContext();
			final String rootSite = context.getContextPath();

			final String rootPath = "http://" + request.getServerName() + ":"
					+ request.getLocalPort() + rootSite;

			final String cssPath = rootPath + "/css/rootCss/";
			final String imgPath = rootPath + "/img/";
			final String jsPath = rootPath + "/js/";

			getConfig().setRootPath(rootPath);
			getConfig().setCssPath(cssPath);
			getConfig().setImgPath(imgPath);
			getConfig().setJsPath(jsPath);
		}

		// Initialization of SESSION context
		final Map<String, Object> mySession = super.getSession();

		// Initialization of default user
		if (mySession.get("user") == null) {
			final User defaultUser = new User();
		
			mySession.put("user", defaultUser);
		}

		// Initialization of current Style
		if (mySession.get("currentStyle") == null) {
			mySession.put("currentStyle", defaultStyle);
		}

		LOG.info("MB - Exit boot()");

		return SUCCESS;
	}

	/**
	 * @return SUCCESS
	 */
	public final String buildlist() {
		// dao ....
		setStyleList(serviceStyle.findAll());

		return SUCCESS;
	}

	/**
	 * @return SUCCESS
	 */
	public final String displayBackOffice() {
		return SUCCESS;
	}

	/**
	 * @return SUCCESS
	 */
	public final String displayFrontOffice() {
		return SUCCESS;
	}

	// IMPLEMENTS METHODS

	/**
	 * @return SUCCESS
	 */
	public final String modifyDefaultStyle() {
		return "";
	}

	/**
	 * @return SUCCESS
	 */
	public final String recoverPassword() {
		return "";
	}

	/**
	 * @return SUCCESS
	 */
	public final String sendEmail() {
		return "";
	}

	/**
	 * @param pDefaultStyle
	 *            the defaultStyle to set
	 */
	public final void setDefaultStyle(final Style pDefaultStyle) {
		this.defaultStyle = pDefaultStyle;
	}

	/**
	 * @param pStyleList
	 *            the styleList to set
	 */
	public final void setStyleList(final List<Style> pStyleList) {
		this.styleList = pStyleList;
	}

	/**
	 * @return SUCCESS
	 */
	public final String switchLocale() {
		// call to fr.cie.blog.controller.results.SamePageResult for the moment
		return SUCCESS;

	}
}